package com.mifengs.mall.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class TwoFourFloAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private TwoFourItemAdapter mTwoFourItemAdapter;
    private FloorBean floorBean;
    public TwoFourFloAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setFloorData(FloorBean floorBean){
        this.floorBean = floorBean;
        notifyDataSetChanged();
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return  new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TwoFourFloorViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_two_four_flo,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TwoFourFloorViewHolder){
            TwoFourFloorViewHolder viewHolder = (TwoFourFloorViewHolder)holder;
            final RecyclerView mRecyclerView = viewHolder.mRecyclerView;
            ImageView five_floor_one_img = viewHolder.five_floor_one_img;
            ImageView five_floor_two_img = viewHolder.five_floor_two_img;
            if(floorBean !=null){
                for(int i = 0;i<floorBean.getAdverts().size();i++ ){
                    AdvertBean adverts = floorBean.getAdverts().get(i);
                    switch (i){
                        case 0:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),five_floor_one_img);
                            break;
                        case 1:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),five_floor_two_img);
                            break;
                    }
                }

                five_floor_one_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(0);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
                five_floor_two_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(1);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });

                List<AdvertBean> tempAdvertBeanList = floorBean.getAdverts();
                final List<AdvertBean> advertBeanList = new ArrayList<AdvertBean>();
                for(int i =2;i<tempAdvertBeanList.size();i++){
                    AdvertBean advertBean = tempAdvertBeanList.get(i);
                    advertBeanList.add(advertBean);
                }
//            mRecyclerView.addItemDecoration(new DividerGridItemDecoration(mContext));
                mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
                mRecyclerView.setNestedScrollingEnabled(false);
                mTwoFourItemAdapter= new TwoFourItemAdapter(R.layout.item_two_four_floor,advertBeanList,mContext,0);
                mRecyclerView.setAdapter(mTwoFourItemAdapter);
                mTwoFourItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AdvertBean adverts = advertBeanList.get(position);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }


    public class TwoFourFloorViewHolder extends RecyclerView.ViewHolder{

        RecyclerView mRecyclerView;
        ImageView five_floor_one_img;
        ImageView five_floor_two_img;
        public TwoFourFloorViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = (RecyclerView)itemView.findViewById(R.id.discount_list);
            five_floor_one_img = (ImageView)itemView.findViewById(R.id.five_floor_one_img);
            five_floor_two_img = (ImageView)itemView.findViewById(R.id.five_floor_two_img);
        }
    }
}
